<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" >

<title>数据结构Stack栈的学习 | 跬步</title>
<meta name="description" content="不积跬步，无以至千里">

<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">

<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.2/css/all.css" integrity="sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr" crossorigin="anonymous">
<link rel="shortcut icon" href="https://skylaugh.gitee.io/blog/favicon.ico?v=1698591240151">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.10.0/katex.min.css">
<link rel="stylesheet" href="https://skylaugh.gitee.io/blog/styles/main.css">



<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script src="https://cdn.bootcss.com/highlight.js/9.12.0/highlight.min.js"></script>

<link rel="stylesheet" href="https://unpkg.com/aos@next/dist/aos.css" />



  </head>
  <body>
    <div id="app" class="main">

      <div class="sidebar" :class="{ 'full-height': menuVisible }">
  <div class="top-container" data-aos="fade-right">
    <div class="top-header-container">
      <a class="site-title-container" href="https://skylaugh.gitee.io/blog">
        <img src="https://skylaugh.gitee.io/blog/images/avatar.png?v=1698591240151" class="site-logo">
        <h1 class="site-title">跬步</h1>
      </a>
      <div class="menu-btn" @click="menuVisible = !menuVisible">
        <div class="line"></div>
      </div>
    </div>
    <div>
      
        
          <a href="/blog/archives" class="site-nav">
            归档
          </a>
        
      
        
          <a href="/blog/tags" class="site-nav">
            标签
          </a>
        
      
        
          <a href="/blog/post/about" class="site-nav">
            关于
          </a>
        
      
    </div>
  </div>
  <div class="bottom-container" data-aos="flip-up" data-aos-offset="0">
    <div class="social-container">
      
        
      
        
      
        
      
        
      
        
      
    </div>
    <div class="site-description">
      不积跬步，无以至千里
    </div>
    <div class="site-footer">
      <a href="mailto:skylaugh@foxmail.com">Email</a> | <a class="rss" href="https://skylaugh.gitee.io/blog/atom.xml" target="_blank">RSS</a>
    </div>
  </div>
</div>


      <div class="main-container">
        <div class="content-container" data-aos="fade-up">
          <div class="post-detail">
            <h2 class="post-title">数据结构Stack栈的学习</h2>
            <div class="post-date">2019-03-05</div>
            
              <div class="feature-container" style="background-image: url('https://skylaugh.gitee.io/blog/post-images/shu-ju-jie-gou-stack-zhan-de-xue-xi.jpg')">
              </div>
            
            <div class="post-content">
              <h3 id="stack">Stack</h3>
<ul>
<li>Java中的Stack是Vector的一个子类。</li>
<li>与队列不同的是，Stack是FILO，先进后出。<br>
<img src="https://skylaugh.gitee.io/blog/post-images/1581409595167.jpg" alt="示意图" loading="lazy"></li>
<li>添加元素方法使用push()，添加到栈顶。</li>
<li>移除栈顶对象的方法pop(),并返回该对象。</li>
<li>search()方法可以在栈中查找元素。</li>
</ul>
<h4 id="一个例子">一个例子</h4>
<h5 id="leetcode-20-有效的括号">leetcode 20. 有效的括号</h5>
<p>给定一个只包括 '('，')'，'{'，'}'，'['，']' 的字符串，判断字符串是否有效。</p>
<p>有效字符串需满足：</p>
<p>左括号必须用相同类型的右括号闭合。<br>
左括号必须以正确的顺序闭合。<br>
注意空字符串可被认为是有效字符串。</p>
<p>示例 1:</p>
<pre><code>输入: &quot;()&quot;
输出: true
</code></pre>
<p>示例 2:</p>
<pre><code>输入: &quot;()[]{}&quot;
输出: true
</code></pre>
<p>示例 3:</p>
<pre><code>输入: &quot;(]&quot;
输出: false
</code></pre>
<p>示例 4:</p>
<pre><code>输入: &quot;([)]&quot;
输出: false
</code></pre>
<p>示例 5:</p>
<pre><code>输入: &quot;{[]}&quot;
输出: true
</code></pre>
<p>来源：力扣（LeetCode）<br>
<a href="https://leetcode-cn.com/problems/valid-parentheses">链接：https://leetcode-cn.com/problems/valid-parentheses</a></p>
<h5 id="解题思路">解题思路</h5>
<p>遍历输入的字符串，如果遇到左括号则push()到栈中，遇到右括号则pop()取出栈顶的值，判断是否于此括号匹配。循环完毕判断栈是否为空栈。</p>
<pre><code>public boolean isValid(String s) {
        Stack&lt;Character&gt; stack = new Stack();
        for (Character a:s.toCharArray()) {
            if (a == '(' || a == '{' || a == '[') {
                stack.push(a);
            }else {
                if (stack.isEmpty()){
                    return false;
                }
                switch (a){
                    case ')':
                        if (stack.pop() != '('){
                            return false;
                        }
                        break;
                    case '}':
                        if (stack.pop() != '{'){
                            return false;
                        }
                        break;
                    case ']':
                        if (stack.pop() != '['){
                            return false;
                        }
                        break;
                        default:
                }
            }
        }
        if (stack.isEmpty()){
            return true;
        }
        return false;
    }
</code></pre>

            </div>
            
              <div class="tag-container">
                
                  <a href="https://skylaugh.gitee.io/blog/tag/AFwlC_qiL/" class="tag">
                    数据结构
                  </a>
                
                  <a href="https://skylaugh.gitee.io/blog/tag/NLraoO2c4/" class="tag">
                    算法
                  </a>
                
              </div>
            
            
              <div class="next-post">
                <div class="next">下一篇</div>
                <a href="https://skylaugh.gitee.io/blog/post/shu-ju-jie-gou-queue-de-xue-xi/">
                  <h3 class="post-title">
                    数据结构Queue的学习
                  </h3>
                </a>
              </div>
            

            

          </div>

        </div>
      </div>
    </div>

    <script src="https://unpkg.com/aos@next/dist/aos.js"></script>

<script type="application/javascript">

AOS.init();

hljs.initHighlightingOnLoad()

var app = new Vue({
  el: '#app',
  data: {
    menuVisible: false,
  },
})

</script>




  </body>
</html>
